package com.lancoo.answer.widget.combinationView;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lancoo.answer.R;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.entity.TaskControlBean;
import com.lancoo.answer.util.DpUtils;
import com.lancoo.answer.util.ExamFontSizeAdjustUtils;
import com.lancoo.answer.util.RichTextUtils;

/**
 * Created by pengmi on 2021/12/14 10:58
 * Description:参考答案查看组合View
 */
public class AnswerAnalysisView extends RelativeLayout {

    private static final String TAG = "AnswerAnalysisView";
    private TextView tv_analysis;
    private final int alignmentType=0;//0-左对齐，1-缩进对齐

    public AnswerAnalysisView(Context context) {
        super(context);
        init(context);
    }

    public AnswerAnalysisView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public AnswerAnalysisView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    private void init(Context context){
        LayoutInflater.from(context).inflate(R.layout.ev_layout_answer_analysis,this);
        TextView tv_title=findViewById(R.id.tv_analysis_des);
        tv_analysis = findViewById(R.id.tv_analysis);
        String str="【";
        float v = tv_title.getPaint().measureText(str);
        Log.e(TAG,"width:"+v);
        tv_title.setTranslationX(-v/2);
        setAlignmentType(alignmentType);
        adjustFontSize();
    }

    public void setAnalysis(String analysis){
        if(TextUtils.isEmpty(analysis)){
            tv_analysis.setText(R.string.ev_item_null);
        }else{
            tv_analysis.setText(RichTextUtils.getQueseAnswerStr(analysis,false,false));
        }
        TaskControlBean taskControlBean = ConstantBean.Companion.getTaskControlBean();
        if(taskControlBean!=null&&taskControlBean.getTrainSence()!=0){
            tv_analysis.setVisibility(GONE);
            findViewById(R.id.tv_analysis_des).setVisibility(GONE);
            Log.e("eee","考试场景不显示答案解析");
        }
    }

    public void setAlignmentType(int alignmentType){
        if(tv_analysis==null){
            return;
        }
        LayoutParams layoutParams = (LayoutParams) tv_analysis.getLayoutParams();
        int dp8= DpUtils.dip2px(getContext(),8);
        int dp2=DpUtils.dip2px(getContext(),2);
        if(alignmentType==1){
            layoutParams.setMarginStart(dp8);
        }else{
            layoutParams.setMarginStart(dp2);
        }
        tv_analysis.setLayoutParams(layoutParams);
    }


    public void adjustFontSize(){
        TextView textView = findViewById(R.id.tv_analysis_des);
        TextView textView1 = findViewById(R.id.tv_analysis);
        ExamFontSizeAdjustUtils.adjustTextViewFontSize(textView,14);
        ExamFontSizeAdjustUtils.adjustTextViewFontSize(textView1,13);
    }
}
